package com.smedi.ismedi.inmail.core.infrastructure.util;

import cn.hutool.core.util.IdUtil;
import com.smedi.ismedi.file.dto.response.FileInfoResponse;

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 通知公告错误消息提示
 *
 * @author jiangqinxuan
 * @date 2021/08/19
 */
public class DownloadFilesToZip {

    public static File downloadFilesToZipFile(List<FileInfoResponse> fileInfoResponseList, String header) throws IOException {
        final File outputFile = File.createTempFile(IdUtil.simpleUUID(), ".zip");
        try(FileOutputStream outputStream = new FileOutputStream(outputFile)){
            Map<String, String> urlAndName = new HashMap<>();
            Set<String> checkSet = new HashSet<>();
            ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
            for (FileInfoResponse fileInfoResponseTemp : fileInfoResponseList) {
                String urlTemp = header + fileInfoResponseTemp.getStorageGroup() + "/" + fileInfoResponseTemp.getPath();
                if(checkSet.add(fileInfoResponseTemp.getName())){ //同样的文件多次上传，只取一个
                    urlAndName.put(urlTemp, fileInfoResponseTemp.getName());
                }
            }
            try {
                urlAndName.forEach((key, value) -> {
                    InputStream inputStream = null;
                    try {
                        inputStream = new URL(key).openStream();
                        ZipEntry entry = new ZipEntry(value);
                        zipOutputStream.putNextEntry(entry);
                        int len;
                        byte[] buffer = new byte[1024];
                        while ((len = inputStream.read(buffer)) != -1) {
                            zipOutputStream.write(buffer, 0, len);
                        }
                        zipOutputStream.closeEntry();
                        inputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (inputStream != null) {
                            try {
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                zipOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    zipOutputStream.flush();
                    zipOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return outputFile;
    }

    public static byte[] downloadFilesToZip(List<FileInfoResponse> fileInfoResponseList,String header) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Map<String, String> urlAndName = new HashMap<>();
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
        for (FileInfoResponse fileInfoResponseTemp : fileInfoResponseList) {
            String urlTemp = header + fileInfoResponseTemp.getStorageGroup() + "/" + fileInfoResponseTemp.getPath();
            urlAndName.put(urlTemp, fileInfoResponseTemp.getName());
        }
        try {
            urlAndName.forEach((key, value) -> {
                InputStream inputStream = null;
                try {
                    inputStream = new URL(key).openStream();
                    ZipEntry entry = new ZipEntry(value);
                    zipOutputStream.putNextEntry(entry);
                    int len;
                    byte[] buffer = new byte[1024];
                    while ((len = inputStream.read(buffer)) != -1) {
                        zipOutputStream.write(buffer, 0, len);
                    }
                    zipOutputStream.closeEntry();
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            zipOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipOutputStream.flush();
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return byteArrayOutputStream.toByteArray();
    }

    private DownloadFilesToZip() {
        throw new IllegalStateException("Utility class");
    }
}
